package cn.yangliu.kindeditor.web;

import cn.yangliu.kindeditor.config.KindEditorConfig;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;

/**
 * 作者 杨柳
 * 时间 2017-09-24 16:54
 * 描述
 */
public class ShowServlet extends HttpServlet {

    private static List<String> MEDIA_SUFFIXS = Arrays.asList(".FLV",".SWF",".MKV",".AVI",".RM",".RMVB",".MPEG",".MPG",".OGG",".OGV",".MOV",".WMV",".MP4",".WEBM",".MP3",".WAV",".MID");
    private static List<String> IMAGE_SUFFIXS = Arrays.asList(".BMP", ".JPG", ".JEPG", ".PNG", ".TIFF", ".GIF", ".PCX", ".TGA", ".EXIF", ".FPX", ".SVG", ".PSD", ".CDR", ".PCD", ".DXF", ".UFO", ".EPS", ".AI", ".RAW");


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        todo(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        todo(request, response);
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        todo(request, response);
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        todo(request, response);
    }
    private void todo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String url = request.getRequestURI();

        //去掉访问前缀
        String path = url.replace(KindEditorConfig.getOtherConfigMap().get("globalUrlPreffix"), "");

        //存储路径
        String pathAndName =KindEditorConfig.getOtherConfigMap().get("globalSavePathPreffix") + path;

        String suffix = url.substring(url.lastIndexOf(".")).toUpperCase();
        response.setContentType("text/html; charset=UTF-8");

        if (IMAGE_SUFFIXS.contains(suffix)) {  //图片格式
            response.setContentType("image/**");
        } else if (MEDIA_SUFFIXS.contains(suffix)) { //媒体格式

            if (suffix.equals(".MP3")) {//音频格式
//            response.setContentType("audio/"+AUDIO_CONTENTTYPE_MAP.get(suffix));
                response.setContentType("audio/mpeg");
            }else { //视频格式
                response.setContentType("video/**");
            }
//
        } else {
            //其他格式
            response.setContentType("multipart/form-data");
        }
        FileInputStream fis = new FileInputStream(new File(pathAndName));
        OutputStream os = response.getOutputStream();
        byte[] buffer = new byte[2048];
        int length;
        while ((length = fis.read(buffer)) != -1) {
            os.write(buffer, 0, length);
        }
    }
}
